<?php
/*
 * Created on
 * Date: 2007-12-4
 * Author: William Yang
 */
class DbError {
	const DB_OK = -1000;
	
	const INSERT_ERROR = -1;
	const UPDATE_ERROR = -2;
	const DELETE_ERROR = -3;
	
	const INVALID_PARAMETER = -4;
	const NO_DB_OPERATION_NEEDED = -5;
	const MANDATORY_COLUMNS_REQUIRED = -6;
	const NO_MYSQL_CONNECTION_AVAILABLE = -7;
	const NO_AUTOINCREMENTED_ID_GENERATED = -8;
	
	const INVALID_DB_QUERY_CONFIGURATION = -9;
	const DUPLICATE_RECORD_ERROR = -10;
	const UNKNOWN_ERROR = -11;
	const INCORRECT_DATE_TIME_VALUE = -12;
	const DATA_TOO_LONG	= -13;
	public static function is_error($code) {
		if (($code == DbError::INSERT_ERROR) ||
			($code == DbError::UPDATE_ERROR) ||
			($code == DbError::DELETE_ERROR) ||
			($code == DbError::INVALID_PARAMETER) ||
			($code == DbError::NO_DB_OPERATION_NEEDED) ||
			($code == DbError::MANDATORY_COLUMNS_REQUIRED) ||
			($code == DbError::NO_MYSQL_CONNECTION_AVAILABLE) ||
			($code == DbError::INVALID_DB_QUERY_CONFIGURATION) ||
			($code == DbError::DUPLICATE_RECORD_ERROR) ||
			($code == DbError::INCORRECT_DATE_TIME_VALUE) ||
			($code == DbError::UNKNOWN_ERROR) ||
			($code == DbError::DATA_TOO_LONG))
			return TRUE;
		else
			return FALSE;
	}
	
	public static function MySqlError2LocalError($errno) {
		if ($errno == 1062)
			/*
			 * Error: 1062 SQLSTATE: 23000 (ER_DUP_ENTRY) Message: Duplicate entry '%s' for key %d. 
			 */
			return DbError::DUPLICATE_RECORD_ERROR;
		else if ($errno == 1292)
			return DbError::INCORRECT_DATE_TIME_VALUE;
		else if ($errno == 1406)
			return DbError::DATA_TOO_LONG;
		else
			return DbError::UNKNOWN_ERROR;
	}
}
?>
